package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.Achievements;
import com.ruoyi.system.domain.TempUserSalaryDto;
import com.ruoyi.system.mapper.AchievementsMapper;
import com.ruoyi.system.service.IAchievementsService;
import org.apache.poi.ss.usermodel.DataFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PayrollMapper;
import com.ruoyi.system.domain.Payroll;
import com.ruoyi.system.service.IPayrollService;
import com.ruoyi.common.core.text.Convert;

/**
 * 工资Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-13
 */
@Service
public class PayrollServiceImpl extends ServiceImpl<PayrollMapper, Payroll> implements IPayrollService
{
    @Autowired
    private PayrollMapper payrollMapper;
    @Autowired
    private IAchievementsService achievementsService;
    /**
     * 查询工资
     * 
     * @param id 工资主键
     * @return 工资
     */
    @Override
    public Payroll selectPayrollById(Long id)
    {
        return payrollMapper.selectPayrollById(id);
    }

    /**
     * 查询工资列表
     * 
     * @param payroll 工资
     * @return 工资
     */
    @Override
    public List<Payroll> selectPayrollList(Payroll payroll)
    {
        return payrollMapper.selectPayrollList(payroll);
    }

    /**
     * 新增工资
     * 
     * @param payroll 工资
     * @return 结果
     */
    @Override
    public int insertPayroll(Payroll payroll)
    {
        payroll.setCreateTime(DateUtils.getNowDate());
        return payrollMapper.insertPayroll(payroll);
    }

    /**
     * 修改工资
     * 
     * @param payroll 工资
     * @return 结果
     */
    @Override
    public int updatePayroll(Payroll payroll)
    {
        payroll.setUpdateTime(DateUtils.getNowDate());
        return payrollMapper.updatePayroll(payroll);
    }

    /**
     * 批量删除工资
     * 
     * @param ids 需要删除的工资主键
     * @return 结果
     */
    @Override
    public int deletePayrollByIds(String ids)
    {
        return payrollMapper.deletePayrollByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除工资信息
     * 
     * @param id 工资主键
     * @return 结果
     */
    @Override
    public int deletePayrollById(Long id)
    {
        return payrollMapper.deletePayrollById(id);
    }

    /**
     * 创建用户发放工资信息
     * @param data 数据
     * @return 工资对象列表
     */
    public List<Payroll> createSalaryMap(List<TempUserSalaryDto> data) {
        List<Payroll> list=new ArrayList<>();
        for (TempUserSalaryDto datum : data) {

            //计算基本工资
            BigDecimal baseSalary=calculateBaseSalary(datum);

            //计算岗位工资
            BigDecimal postSalary=calculatePostSalary(datum);

            //计算绩效工资
            BigDecimal meritSalary=calculateMeritSalary(datum);

            //计算生活津贴
            BigDecimal subsistenceAllowance=calculateSubsistenceAllowance(datum);

            //计算加班工资
            BigDecimal overtimeSalary=calculateOvertimeSalary(datum);

            //计算计件工资
            BigDecimal pieceSalary=calculatePieceSalary(datum);

            //计算总工资
            BigDecimal totalSalary=new BigDecimal(0);
            totalSalary=totalSalary.add(baseSalary)
                    .add(postSalary)
                    .add(meritSalary)
                    .add(subsistenceAllowance)
                    .add(overtimeSalary)
                    .add(pieceSalary);

            //计算实发工资
            BigDecimal netSalary=new BigDecimal(0);
            netSalary=totalSalary;

            Date date = new Date();
            //生成工资对象
            Payroll payroll=new Payroll();
            payroll.setBaseSalary(baseSalary);
            payroll.setPostSalary(postSalary);
            payroll.setMeritSalary(meritSalary);
            payroll.setSubsistenceAllowance(subsistenceAllowance);
            payroll.setOvertimeSalary(overtimeSalary);
            payroll.setPieceSalary(pieceSalary);
            payroll.setTotalSalary(totalSalary);
            payroll.setNetSalary(netSalary);
            payroll.setUserId(datum.getUserId());
            payroll.setUserName(datum.getUserName());
            payroll.setAchievementsId(datum.getId());
            payroll.setSalaryDate(datum.getAchievementsDate());
            payroll.setRemark(datum.getRemark());
            payroll.setCreateTime(date);
            payroll.setCreateBy(ShiroUtils.getSysUser().getUserName());
            payroll.setUpdateTime(date);
            payroll.setUpdateBy(ShiroUtils.getSysUser().getUserName());
            list.add(payroll);
        }
        saveBatch(list);
        return list;
    }
    /**
     * 计算基本工资
     * @param data 员工工资临时存储对象
     * @return 基本工资
     */
    @Override
    public BigDecimal calculateBaseSalary(TempUserSalaryDto data) {
        return data.getFixedWageStandard();
    }
    /**
     * 计算岗位工资
     * @param data 员工工资临时存储对象
     * @return 岗位工资
     */
    @Override
    public BigDecimal calculatePostSalary(TempUserSalaryDto data) {
        BigDecimal postSalaryStandard = data.getPostSalaryStandard();
        postSalaryStandard=postSalaryStandard.add(data.getLevelMoney().multiply(new BigDecimal(data.getLavel())));
        return postSalaryStandard
                .divide(new BigDecimal(data.getFullAttendanceDays()),2, RoundingMode.HALF_UP)
                .multiply(new BigDecimal(data.getAttendanceDays()));
    }
    /**
     * 计算绩效工资
     * @param data 员工工资临时存储对象
     * @return 绩效工资
     */
    @Override
    public BigDecimal calculateMeritSalary(TempUserSalaryDto data) {
        if (data.getScore().doubleValue()>=90){
            return data.getPerformanceSalaryStandard();
        }else {
            return data.getPerformanceSalaryStandard().multiply(data.getScore()).divide(new BigDecimal(100));
        }
    }
    /**
     * 计算生活津贴
     * @param data 员工工资临时存储对象
     * @return 生活津贴
     */
    @Override
    public BigDecimal calculateSubsistenceAllowance(TempUserSalaryDto data) {
        return data.getSubsistenceAllowanceStandard()
                .divide(new BigDecimal(data.getFullAttendanceDays()),2, RoundingMode.HALF_UP)
                .multiply(new BigDecimal(data.getAttendanceDays()));
    }
    /**
     * 计算加班工资
     * @param data 员工工资临时存储对象
     * @return 加班工资
     */
    @Override
    public BigDecimal calculateOvertimeSalary(TempUserSalaryDto data) {
        return data.getBasicWageStandard()
                .divide(new BigDecimal(data.getFullAttendanceDays()),2, RoundingMode.HALF_UP)
                .multiply(new BigDecimal(data.getOvertimeDays()));
    }
    /**
     * 计算计件工资
     * @param data 员工工资临时存储对象
     * @return 计件工资
     */
    @Override
    public BigDecimal calculatePieceSalary(TempUserSalaryDto data) {
        return data.getPieceMoney().multiply(new BigDecimal(data.getProductionQuantity()));
    }
}
